package com.gusti.turnos.controller.views;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.gusti.turnos.form.LoginForm;
import com.gusti.turnos.model.Appointment;
import com.gusti.turnos.model.User;
import com.gusti.turnos.response.views.LoginResponse;
import com.gusti.turnos.services.IAppointmentService;
import com.gusti.turnos.services.IAuthenticationService;
import com.gusti.turnos.services.IEventService;
import com.gusti.turnos.services.IUserService;
import com.gusti.turnos.util.ServerUtil;

/**
 * 
 * @author gusti
 *
 */
@Controller
public class ViewsController {

	@Autowired
	IAuthenticationService authenticationService;
	@Autowired
	IUserService userService;
	@Autowired
	IAppointmentService appointmentService;
	@Autowired
	IEventService eventService;

	//GET METHOD
	@RequestMapping(value="/home.do", method=RequestMethod.GET)
	public ModelAndView home( ModelMap model ) {

		//always clean session
		authenticationService.logout();

		//always return the login page
		return loginFormPage( model );
	}

	@RequestMapping(value="/retrieveUsers.do", method=RequestMethod.GET)
	public ModelAndView retrieveUsers( ModelMap model ) {

		//validate if someone is logged in
		if( !ServerUtil.getInstance().isValidSession() ) {
			return loginFormPage( model );
		}

		return new ModelAndView( "listUsers" );
	}

	@RequestMapping(value="/addEditUser.do", method=RequestMethod.GET)
	public ModelAndView addEditUser( ModelMap model, HttpServletRequest request ) {

		//validate if someone is logged in
		if( !ServerUtil.getInstance().isValidSession() ) {
			return loginFormPage( model );
		}

		User user = new User();
		if( request.getParameter( "id" ) != null ) {
			user = userService.retrieveUser( Long.valueOf( request.getParameter( "id" ) ) );
		}
		model.put( "user", user );

		return new ModelAndView( "addEditUserForm" );
	}

	@RequestMapping(value="/retrieveAppointments.do", method=RequestMethod.GET)
	public ModelAndView retrieveAppointments( ModelMap model ) {

		//validate if someone is logged in
		if( !ServerUtil.getInstance().isValidSession() ) {
			return loginFormPage( model );
		}

		return new ModelAndView( "listAppointments" );
	}

	@RequestMapping(value="/addEditAppointment.do", method=RequestMethod.GET)
	public ModelAndView addEditAppointment( ModelMap model, HttpServletRequest request ) {

		//validate if someone is logged in
		if( !ServerUtil.getInstance().isValidSession() ) {
			return loginFormPage( model );
		}

		Appointment appointment = new Appointment();
		if( request.getParameter( "id" ) != null ) {
			appointment = appointmentService.retrieveAppointment( Long.valueOf( request.getParameter( "id" ) ) );
		}
		model.put( "appointment", appointment );

		return new ModelAndView( "addEditAppointmentForm" );
	}

	//POST METHOD
	@RequestMapping(value="/home.do", method=RequestMethod.POST)
	public ModelAndView login( @Valid LoginForm loginForm, BindingResult result ) {

		if( result.hasErrors() ) {
			//return the login page and show the validation errors
			return new ModelAndView( "loginForm" );
		}

		//do authenticate
		LoginResponse loginResponse = authenticationService.login( loginForm );

		ModelAndView modelAndView = new ModelAndView();
		if( loginResponse.isSuccess() ) {
			modelAndView.setViewName( ServerUtil.getInstance().getCurrentUserHomePage() );
		} else {
			modelAndView.setViewName( "loginForm" );
		}

		//always return a view and the loginResponse object
		modelAndView.addObject( "loginResponse", loginResponse );
		return modelAndView;
	}

	private ModelAndView loginFormPage( ModelMap model ) {

		//create new LoginForm
		model.addAttribute( "loginForm", new LoginForm() );

		//always return the login page
		return new ModelAndView( "loginForm" );
	}
}
